package pc

import (
	. "chess_platform/internal/common"
	"chess_platform/models/client"
	"chess_platform/models/pc"
	"fmt"
)

//[pc]player:玩家管理
type PlayerController struct {
	GrantController
}

func (b *PlayerController) URLMapping() {
	//player:玩家管理
	b.Mapping("ForcePlayerUpgrade", b.ForcePlayerUpgrade)
	b.Mapping("Disable", b.Disable)
	b.Mapping("Enable", b.Enable)
	b.Mapping("List", b.List)

	//diamond:玩家钻石管理
	b.Mapping("AddCard", b.AddCard)
	b.Mapping("DecreaseCard", b.DecreaseCard)
}

// @Title 强制玩家更新版本
// @Description 强制玩家更新版本
// @Param   uid            body    int     true      "玩家uid"
// @Success 0 {string} 状态码
// @router /player/force-player-upgrade [post]
func (b *PlayerController) ForcePlayerUpgrade() {
	type Params struct {
		Uid int64 `json:"uid" valid:"Required"`
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}
	if !b.ValidParams(&p) {
		return
	}

	ok := client.BroadCastMsgToLobby(0, GenBroadcastMsg(ModuleSys, ActionForceUpgrade, ""), p.Uid)
	if !ok {
		b.ResponseFail("游戏服务器返回失败,请联系开发人员")
		return
	}
	b.ResponseSuccess()
	msg := fmt.Sprintf("强制更新玩家(uid:%v)版本", p.Uid)
	b.NewLog(LogPlayer, OpUpdate, msg, map[string]interface{}{})
}

// @Title 冻结玩家
// @Description 冻结玩家
// @Param   uid            body    int     true      "玩家uid"
// @Param   frozen_reason  body    string  true      "冻结原因"
// @Success 0 {string} 状态码
// @router /player/disable [post]
func (b *PlayerController) Disable() {
	type Params struct {
		Uid          int64  `json:"uid"`
		FrozenReason string `json:"frozen_reason"`
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}
	b.Valid.Required(p.Uid, "uid").Message("缺少用户id")
	b.Valid.Required(p.FrozenReason, "frozen_reason").Message("冻结原因不能为空")
	if !b.VerifyParamsError() {
		return
	}
	updateData := map[string]interface{}{
		"frozen":        1,
		"frozen_reason": p.FrozenReason,
	}
	_, err := client.UpdateUser(updateData, p.Uid)
	if err != nil {
		LogPc.Error(b.DatabaseError(err.Error()))

		return
	}
	b.ResponseSuccess()
	msg := fmt.Sprintf("冻结玩家(uid:%v),冻结原因:%v", p.Uid, p.FrozenReason)
	b.NewLog(LogPlayer, OpUpdate, msg, map[string]interface{}{})
}

// @Title 解冻玩家
// @Description 解冻玩家
// @Param   uid            body    int     true      "玩家uid"
// @Success 0 {string} 状态码
// @router /player/enable [post]
func (b *PlayerController) Enable() {
	type Params struct {
		Uid int64 `json:"uid" valid:"Required"`
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}
	if !b.ValidParams(&p) {
		return
	}
	updateData := map[string]interface{}{
		"frozen": 0,
	}

	_, err := client.UpdateUser(updateData, p.Uid)
	if err != nil {
		LogPc.Error(b.DatabaseError(err.Error()))
		return
	}
	b.ResponseSuccess()
	msg := fmt.Sprintf("解冻玩家(uid:%v)", p.Uid)
	b.NewLog(LogPlayer, OpUpdate, msg, map[string]interface{}{})
}

// @Title 获取玩家列表
// @Description 获取玩家列表
// @Param   page_size    body   int     true       "每页显示数量"
// @Param   page_index   body   int     true       "第几页"
// @Param   uid          body   int     false      "搜索玩家id,[1,2,3,4]"
// @Param   name         body   string  false      "搜索玩家昵称"
// @Param   phone        body   string  false      "搜索玩家手机号"
// @Param   rtime        body   string  false      "搜索玩家注册"
// @Param   order_name 	 body   string  false      "排序名字"
// @Param   order        body   int     false      "1-升序,2-降序"
// @Success 0 {object} []client.User 用户列表
// @router /player/list [post]
func (b *PlayerController) List() {
	type Params struct {
		ListParams
		ListCtUserParams
		OrderParams
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}
	users, count, err := pc.ListPlayerInfo(p.ListParams, p.ListCtUserParams, p.OrderParams)
	if err != nil {
		LogPc.Error(b.DatabaseError(err.Error()))
		return
	}

	b.Res.Data = users
	b.Res.Count = count
	b.ResponseSuccess()
}

//----------------------------------玩家钻石管理-----------------------------------

// @Title 增加玩家钻石
// @Description 增加玩家钻石
// @Param   uid            body    int     true      "玩家uid"
// @Param   card_num       body    string  true      "钻石数量"
// @Success 0 {string} 状态码
// @router /diamond/add-card [post]
func (b *PlayerController) AddCard() {
	type Params struct {
		Uid     int64 `json:"uid" valid:"Required"`
		CardNum int64 `json:"card_num"`
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}
	b.Valid.Min(p.CardNum, 1, "card_num").Message("请输入大于1钻石数")
	if !b.ValidParams(&p) {
		return
	}
	cardNum := MathAbs(p.CardNum)
	ok := client.ChangeAccountValueFromLobby(p.Uid, cardNum,
		0, 0, ACCTRAN_OPER_TYPE_SysChange, 0)
	if !ok {
		b.ResponseFail("游戏服异常,修改失败,请联系开发人员")
		return
	}
	err := pc.DoSystemDiamond(SysClientGiveUid, -cardNum, p.Uid, SysOpHandOut, b.Uid, Player)
	if err != nil {
		LogPc.Error(fmt.Sprintf("从客户端系统赠送帐号:%v扣钻石数:%v到玩家(ID:%v)失败,操作类型为:%v,失败日志:%v",
			SysClientGiveUid, cardNum, p.Uid, SysOpHandIn, err))
	}

	b.ResponseSuccess()
	msg := fmt.Sprintf("增加玩家(uid:%v)钻石(%v)", p.Uid, cardNum)
	b.NewLog(LogPlayer, OpUpdate, msg, map[string]interface{}{})
}

// @Title 减少玩家钻石
// @Description 减少玩家钻石
// @Param   uid            body    int     true      "玩家uid"
// @Param   card_num       body    string  true      "钻石数量,传负值,为减少钻石数量"
// @Success 0 {string} 状态码
// @router /diamond/decrease-card [post]
func (b *PlayerController) DecreaseCard() {
	type Params struct {
		Uid     int64 `json:"uid" valid:"Required"`
		CardNum int64 `json:"card_num"`
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}
	b.Valid.Required(p.CardNum, "card_num").Message("不能输入0颗钻石数")
	if !b.VerifyParamsError() {
		return
	}
	cardNum := MathAbs(p.CardNum)

	ok := client.ChangeAccountValueFromLobby(p.Uid, -cardNum,
		0, 0, ACCTRAN_OPER_TYPE_SysChange, 0)
	if !ok {
		b.ResponseFail("游戏服异常,修改失败,请联系开发人员")
		return
	}
	err := pc.DoSystemDiamond(SysClientGiveUid, -cardNum, p.Uid, SysOpHandIn, b.Uid, Player)
	if err != nil {
		LogPc.Error(fmt.Sprintf("从玩家(ID:%v)回收钻石数:%v到客户端系统赠送帐号:%v失败,操作类型为:%v,失败日志:%v",
			p.Uid, cardNum, SysClientGiveUid, SysOpHandIn, err))
	}
	b.ResponseSuccess()
	msg := fmt.Sprintf("减少玩家(uid:%v)钻石(%v)", p.Uid, cardNum)
	b.NewLog(LogPlayer, OpUpdate, msg, map[string]interface{}{})
}
